#!/usr/bin/perl
#espsf0boss.pl
#espsf0boss - calculate f0 with the ESPS toolkit for the BOSS corpus tool "addf0".
use locale;
use POSIX;
use FindBin qw($Bin);
use lib "$Bin/../libs";
use config;

#Version: 1.1
#@author: unknown
#@author: Igor Jauk

#Changelist:
#config class usage
#variable size, step and rate parameters
#raw dir specificable through parameters
#sd and f0 folders created in the execution fodler of the program


my $USAGE = "perl espsf0boss.pl -rdir RAWDIR [-size WINDOWSIZE {in seconds; default: 0.02} -step STEPSIZE {in seconds, default: 0.005} -rate SAMPLINGRATE {in Hz; default: 16000} -h]";

my $cl = config->new(\@ARGV);
if ($cl->optionflag("-h")) {
	print $USAGE."\n";
	exit;
}
# Establish the window size and frame step for periodic analyses.

my $size=".02";
my $step=".005";
my $rate="16000";
my $rootfolder = "";

if ($cl->optionflag("-size")) {
	$size = $cl->optionarg("-size");
} else {
	print STDERR "Using default window size value: $size\n";
}

if ($cl->optionflag("-step")) {
	$step = $cl->optionarg("-step");
} else {
	print STDERR "Using default step value: $step\n";
}

if ($cl->optionflag("-rate")) {
	$rate = $cl->optionarg("-rate");
} else {
	print STDERR "Using default rate value: $rate\n";
}

if (!$cl->optionflag("-rdir")) {
	die "error: please specify a root directory which contains the .raw corpus audio files\n$USAGE\n";
}

my $rdir = $cl->optionarg("-rdir");

opendir (DIR, "$rdir")or die "can't open the raw directory\n!$!\n";

if (!-d "sd") {
    mkdir "sd";
}
if (!-d "f0") {
    mkdir "f0";
}
if (!-d "sd") {
  die "Couldn't create sd!\n";
}
if (!-d "f0") {
  die "Couldn't create f0!\n";
}
while ($file = readdir(DIR)) {
  if (!($file =~ s/\.raw$//)) {
    next;
  }
  $sd = "sd/$file.sd";
  $sdtmp = "sd/$file.tmp.sd";
  $rc = "sd/$file.rc";
  $f0 = "sd/$file.f0";
  $f0_ascii = "f0/$file.f0_ascii";
  $res = "sd/$file.res";
  $resm = "sd/$file.resm";
  $lab = "sd/$file.lab";
  $pe = "sd/$file.pe";
  if (!-s "$sd") {
    print "Create $sd\n";
    system ("btosps -f$rate -c $file $ARGV[0]/raw/$file.raw $sdtmp");
    system ("rem_dc $sdtmp $sd");
    unlink ("$sdtmp");
  }
  if (!-s "$sd") { die("Couldn't create $sd\n"); }
  print "Calculate F0 for $sd\n";
  # Determine the sample rate of the original speech file.
  print "Determine the sample rate of the original speech file: ";
  $sf = `hditem -i record_freq $sd`;
  print "$sf\n";
  # Get analysis step size and window length in samples.
  print "Get analysis step size and window length in samples: ";
  $ssize=`echo "$sf $size * p q" | dc`;
  $sstep=`echo "$sf $step * p q" | dc`;
  print "$ssize $sstep\n";
  # Standard rule-of-thumb computation for LPC order.
  #  print "Standard rule-of-thumb computation for LPC order: ";
  #  $order=`echo "$sf 1000 / 2 + p q" | dc`;
  #  print "$order\n";
  # Compute reflection coefficients using a standard set of parameters
  #print "Compute reflection coefficients using a standard set of parameters.\n";
  #  system("refcof -z -r1:1000000 -e.97 -x0 -wHANNING -l$ssize -S$sstep \ -o$order $sd $rc");
#  # Get a high-resolution estimate of F0 and a reasonably accurate
#  #  voicing-state estimate.
  print "Get a high-resolution estimate of F0\n";
  system("get_f0 -i $step $sd $f0");
  system("fea_print fea_print.layout $f0 > $f0_ascii");
#  # Compute the LPC residual (approximates the glottal flow derivative).
#  system("get_resid -a 1 -i 0.0 $sd $rc $res");
#  # Blank out the residual signal in the unvoiced regions.
#  system("mask $f0 $res $resm");
#  # Find the points of glottal closure in the voiced regions.
#  system("epochs -p -o $lab $resm $pe");
}
close DIR;
